package cn.iocoder.yudao.module.bpm.message.rabbitmq.custom.processInstance;


import cn.iocoder.yudao.module.bpm.message.consts.ProcessInstanceStreamKey;
import cn.iocoder.yudao.module.bpm.message.handle.ProcessInstanceMessageHandle;
import cn.iocoder.yudao.module.bpm.message.rocketmq.dto.RocketMqBpmProcessInstanceCancelledMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;

@Slf4j
@RequiredArgsConstructor
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(
                        // 在 Queue 的名字上，使用 UUID 生成其后缀。这样，启动的 Consumer 的 Queue 不同，以达到广播消费的目的
                        name = ProcessInstanceStreamKey.PROCESS_INSTANCE_CLEAN_MSG_GROUP +"_" + "${spring.application.name}",
                        // Consumer 关闭时，该队列就可以被自动删除了
                        autoDelete = "true"
                ),
                exchange = @Exchange(
                        name = ProcessInstanceStreamKey.PROCESS_INSTANCE_CLEAN_MSG_STREAM,
                        type = ExchangeTypes.TOPIC,
                        declare = "false"
                )
        )
)
public class RabbitMqProcessInstanceCancelledCustom {


    private final ProcessInstanceMessageHandle processInstanceMessageHandle;

    @RabbitHandler
    public void onMessage(RocketMqBpmProcessInstanceCancelledMessage message) {
        try {
            log.info("消费消息：{}", message);
            processInstanceMessageHandle.onProcessInstanceCleanMsg(message.getBpmProcessMessage());
        } catch (Exception e) {
            log.error("MQ消费失败", e);
            throw new RuntimeException(e.getMessage());
        }
    }

}
